package br.com.dreamsource.mobile.worders.forms.core;

import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;
import br.com.dreamsource.mobile.jmesql.io.File;
import br.com.dreamsource.mobile.worders.WOrders;
import br.com.dreamsource.mobile.worders.pojo.UserPOJO;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Ticker;

/**
 *
 * @author Elizeu Nogueira da Rosa Jr.
 * @version 0.1
 * @date 20.05.2007
 *
 */

public class DatabaseOpen extends Form {
    
    private Ticker tickerInit;
    private UserPOJO userPOJO;
    
    private WOrders wOrders;
    private Form backForm;
    
    public DatabaseOpen(UserPOJO userPOJO, WOrders wOrders, Form backForm) {
        super(null, null);
        this.userPOJO = userPOJO;
        this.wOrders = wOrders;
        this.backForm = backForm;
        this.createComponents();
        Display.getDisplay(wOrders).setCurrent(this);
        this.openDatabase();
    }
    
    private Ticker getTickerInit() {
        if (tickerInit == null) {
            tickerInit = new Ticker("Inicializando aplicativo");
        }
        return tickerInit;
    }    
    
    private void createComponents() {
        this.setTicker(this.getTickerInit());
    }
    
    private void openDatabase() {
        try {
            DatabaseManager dm = new DatabaseManager(this.userPOJO);
            wOrders.serUserPOJO(this.userPOJO);
            Display.getDisplay(wOrders).setCurrent(wOrders.getMenu());
            wOrders.destroyForm(backForm);
            wOrders.destroyForm(this);
        } catch (SQLException sqlEx) {
            Display.getDisplay(wOrders).setCurrent(
            wOrders.getSystemMessage("ERRO", sqlEx.getMessage(), this.backForm));
        }
    }
    
}
